package com.cby.library.common.util

import android.content.Context
import android.content.res.Configuration
import android.graphics.Point
import android.os.Build
import android.util.Log
import android.view.Display
import android.view.WindowManager

/**
 * 屏幕相关信息工具类
 *
 * @author chenbaoyang
 */
object ScreenUtils {
    /**
     * 描述：dip转换px
     * @param context
     * @param dipValue
     * @return
     */
    @JvmStatic
    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    /**
     * 描述：px转换dip
     * @param context
     * @param pxValue
     * @return
     */
    @JvmStatic
    fun px2dip(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        Log.i("TESTTTT", "scale=$scale")
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     * 从sp到px
     *
     * @param spValue
     * @return
     */
    @JvmStatic
    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }

    /**
     * px 2 sp
     *
     * @param pxValue
     * @return
     */
    @JvmStatic
    fun px2sp(context: Context, pxValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

    /**
     * 获取屏幕高度
     * @param context
     * @return
     */
    @JvmStatic
    fun getScreenHeight(context: Context): Int {
        var screenHeight = 0
        screenHeight = if (Build.VERSION.SDK_INT >= 13) {
            val display =
                getDefaultDisplay(
                    context
                )
            val size = Point()
            display.getSize(size)
            size.y
        } else {
            val dm = context.resources.displayMetrics
            dm.heightPixels
        }
        return screenHeight
    }

    /**
     * 获取屏幕宽度
     * @param context
     * @return
     */
    @JvmStatic
    fun getScreenWidth(context: Context): Int {
        var screenWidth = 0
        screenWidth = if (Build.VERSION.SDK_INT >= 13) {
            val display =
                getDefaultDisplay(
                    context
                )
            val size = Point()
            display.getSize(size)
            size.x
        } else {
            val dm = context.resources.displayMetrics
            dm.widthPixels
        }
        return screenWidth
    }

    private fun getDefaultDisplay(context: Context): Display {
        val wm =
            context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        return wm.defaultDisplay
    }

    /**
     * 返回当前屏幕是否为竖屏。
     * @param context
     * @return 当且仅当当前屏幕为竖屏时返回true,否则返回false。
     */
    fun isScreenOriatationPortrait(context: Context): Boolean {
        return context.resources
            .configuration.orientation == Configuration.ORIENTATION_PORTRAIT
    }
}